package com.example.servlet;

import com.example.bean.Student;
import com.example.dao.StudentDeleteDao;
import com.example.dao.StudentDeleteDaoImp;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "DeleteServlet", value = "/delete")
public class DeleteServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取需要删除的学员的姓名
        String name = request.getParameter("name");
        System.out.println("获取到的需要删除的学员的姓名为:"+name);

        //2.将姓名打包给Dao层进行处理
        Student student=new Student(name);
        StudentDeleteDao studentDeleteDao=new StudentDeleteDaoImp();
        int res = studentDeleteDao.getStudentDelete(student);

        response.setContentType("html/text;charset=utf-8");
        if(1==res){
            request.getSession().setAttribute("success","删除成功!");
            response.sendRedirect("delete.jsp");
            System.out.println("学员:"+name+"的信息删除成功!");
        }else{
            request.setAttribute("fail","删除失败!");
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("delete.jsp");
            requestDispatcher.forward(request,response);
            System.out.println("学员:"+name+"的信息不存在,删除失败!");
        }

    }
}
